package com.huqing.icu.service.admin.impl;

import com.huqing.icu.base.PagingDto;
import com.huqing.icu.dao.HouseDao;
import com.huqing.icu.dao.HouseInteractionPanelDao;
import com.huqing.icu.dto.HouseDto;
import com.huqing.icu.dto.HouseQueryDto;
import com.huqing.icu.enums.BusinessConstants;
import com.huqing.icu.enums.HouseApproveStatusEnum;
import com.huqing.icu.exceptions.BusinessException;
import com.huqing.icu.inout.in.admin.AdminHouseListQueryReq;
import com.huqing.icu.inout.in.admin.HouseApproveReq;
import com.huqing.icu.inout.out.admin.AdminHouseListResp;
import com.huqing.icu.model.HouseApproveRecord;
import com.huqing.icu.model.HouseInteractionPanel;
import com.huqing.icu.service.admin.IAdminHouseService;
import com.huqing.icu.utils.CopyUtils;
import com.huqing.icu.utils.PageUtils;
import com.huqing.icu.utils.ParamValidateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 后台房源接口实现类
 * @Author huqing
 * @Date 2025/9/14 0:01
 **/
@Service
public class AdminHouseServiceImpl implements IAdminHouseService {

    @Autowired
    private HouseDao houseDao;
    @Autowired
    private HouseInteractionPanelDao houseInteractionPanelDao;

    /**
     * 查询房源列表
     * @param queryReq
     * @return
     */
    @Override
    public PagingDto<AdminHouseListResp> queryList(AdminHouseListQueryReq queryReq) {
        HouseQueryDto houseQueryDto = CopyUtils.copy(queryReq, HouseQueryDto.class);
        int startIndex = PageUtils.getPageStartIndex(queryReq.getPageIndex(), queryReq.getPageSize());
        houseQueryDto.setStartIndex(startIndex);
        //1、根据条件查询举报记录数量
        Integer count = houseDao.getNumByCondition(houseQueryDto);

        if (count == 0) {
            return new PagingDto<>(Collections.emptyList(), 0, 0);
        }

        //2、根据条件查询举报记录列表
        List<HouseDto> reportRecordDtoList = houseDao.selectByCondition(houseQueryDto);

        List<Long> idList = reportRecordDtoList.stream().map(HouseDto::getId).collect(Collectors.toList());
        //3、查询这些房源的审核状态
        List<HouseApproveRecord> houseApproveRecordList = houseDao.selectApproveRecordByHouseIdList(idList);
        Map<Long, HouseApproveRecord> houseApproveRecordMap = houseApproveRecordList.stream().collect(Collectors.toMap(HouseApproveRecord::getHouseId, p -> p));

        List<AdminHouseListResp> houseListRespList = CopyUtils.copyOriginListToDestList(reportRecordDtoList, AdminHouseListResp.class);
        for (AdminHouseListResp resp : houseListRespList) {

            //4.1 填充审核状态的信息
            HouseApproveRecord houseApproveRecord = houseApproveRecordMap.get(resp.getId());
            resp.setApproveStatus(houseApproveRecord.getApproveStatus());
            resp.setApproveContent(houseApproveRecord.getApproveContent());
            resp.setApproveUserId(houseApproveRecord.getApproveUserId());
            resp.setApproveTime(houseApproveRecord.getApproveTime());
        }

        return new PagingDto<>(houseListRespList, PageUtils.getTotalPage(count, queryReq.getPageSize()), count);
    }

    /**
     * 查询房源详情
     * @param id
     * @return
     */
    @Override
    public AdminHouseListResp queryDetail(Long id) {

        HouseDto houseDto = houseDao.selectById(id);
        if (houseDto == null) {
            throw new BusinessException(BusinessConstants.DATA_NOT_EXIST.getCode(), "房源不存在，请检查");
        }

        AdminHouseListResp listResp = CopyUtils.copy(houseDto, AdminHouseListResp.class);
        //2、查询点赞量、收藏量等
        HouseInteractionPanel houseInteractionPanel = houseInteractionPanelDao.selectByHouseId(id);

        if (houseInteractionPanel != null) {

            //3.1、设置浏览量、点击量、点赞量、收藏量
            listResp.setActualViewCount(houseInteractionPanel.getActualViewCount());
            listResp.setActualViewUserCount( houseInteractionPanel.getActualViewUserCount());
            listResp.setActualLikeCount( houseInteractionPanel.getActualLikeCount());
            listResp.setActualFavoriteCount(houseInteractionPanel.getActualFavoriteCount());
            listResp.setActualCommentCount(houseInteractionPanel.getActualCommentCount());
        }

        return listResp;
    }

    /**
     * 审核房源
     * @param houseApproveReq
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer approve(HouseApproveReq houseApproveReq) {

        //1、查询这条审核记录
        HouseApproveRecord houseApproveRecord = houseDao.selectApproveRecordById(houseApproveReq.getId());
        if (houseApproveRecord == null || !HouseApproveStatusEnum.NOT_APPROVE.getCode().equals(houseApproveRecord.getApproveStatus())) {
            ParamValidateUtils.throwInternalIllegalExp("当前状态不允许操作");
        }

        //2、修改审核记录
        houseApproveRecord.setApproveStatus(houseApproveReq.getApproveStatus());
        houseApproveRecord.setApproveContent(houseApproveReq.getApproveContent());
        houseApproveRecord.setApproveUserId(houseApproveReq.getApproveUserId());
        houseDao.updateApproveRecord(houseApproveRecord);
        return 1;
    }
}
